package com.hsj.goldstore.mvp.view;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.content.Loader;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.hsj.goldstore.R;
import com.hsj.goldstore.base.BaseMvpActivity;
import com.hsj.goldstore.base.PresenterFactory;
import com.hsj.goldstore.base.PresenterLoader;
import com.hsj.goldstore.bean.LoginBean;
import com.hsj.goldstore.comm.ConstantType;
import com.hsj.goldstore.mvp.present.LoginPresent;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/3/25.
 */

public class LoginActivity extends BaseMvpActivity<LoginPresent, LoginView> implements LoginView {


    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.btn_right)
    TextView btnRight;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_pwd)
    EditText etPwd;
    @BindView(R.id.btn_login)
    Button btnLogin;
    @BindView(R.id.tv_forget_pwd)
    TextView tvForgetPwd;



    @Override
    protected int getResouseId() {
        return R.layout.activity_login;
    }

    @Override
    protected void initView() {
        btnBack.setVisibility(View.GONE);
        tvTitle.setText(getString(R.string.login));
        btnRight.setText(getString(R.string.regist));
    }

    @Override
    protected void initData() {
     presenter.init();
    }

    @Override
    public Loader<LoginPresent> onCreateLoader(int id, Bundle args) {
        return new PresenterLoader<>(this, new PresenterFactory<LoginPresent>() {
            @Override
            public LoginPresent create() {
                return new LoginPresent();
            }
        });
    }

    public static void getIntance(Context context, Bundle bundle) {
        Intent intent = new Intent(context, LoginActivity.class);
        context.startActivity(intent);
    }

    @Override
    public String getPhone() {
        return etPhone.getText().toString().trim();
    }

    @Override
    public void setPhone(String phone) {
        etPhone.setText(phone);
    }

    @Override
    public String getPWD() {
        return etPwd.getText().toString().trim();
    }

    @Override
    public void setPWD(String pwd) {
        etPwd.setText(pwd);
    }

    @Override
    public void gotoMainActivity() {
        MainActivity.getIntantce(this, MainActivity.class);
        finish();
    }

    @Override
    public void gotoRegistActivity() {
        RegisterActivity.getIntantce(this, RegisterActivity.class);
    }

    @Override
    public void gotoForgetPasswordActivity() {

        ForgetPasswordActivity.getIntantce(this, ForgetPasswordActivity.class);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    /*
    * 保存用户信息
    * */
    @Override
    public void saveUserInfo(String phone, String pwd,LoginBean loginBean) {
       SharedPreferences spPut = getSharedPreferences(ConstantType.SP_USER,
               Context.MODE_PRIVATE);
       SharedPreferences.Editor editor = spPut.edit();
       editor.putString(ConstantType.PHONE, phone);
       editor.putString(ConstantType.PWD, pwd);
        if (null != loginBean) {
            editor.putString(ConstantType.ACCESS_TOKEN, loginBean.getAccess_token());
            editor.putString(ConstantType.TOKEN_TYPE, loginBean.getToken_type());
            editor.putLong(ConstantType.EXPIRES_IN, loginBean.getExpires_in());
        }
       editor.commit();
    }

    @Override
    public void showUserInfo() {
        SharedPreferences spGet = getSharedPreferences(ConstantType.SP_USER, Context.MODE_PRIVATE);
        String phone = spGet.getString(ConstantType.PHONE,"");
        String pwd = spGet.getString(ConstantType.PWD,"");
        if (!TextUtils.isEmpty(phone)) {
            setPhone(phone);
        }
        if (!TextUtils.isEmpty(pwd)) {
            setPWD(pwd);
        }
    }

    @OnClick({R.id.btn_right, R.id.btn_login, R.id.tv_forget_pwd})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_right:
                gotoRegistActivity();
                break;
            case R.id.btn_login:
                presenter.login();
                break;
            case R.id.tv_forget_pwd:
                gotoForgetPasswordActivity();
                break;
        }
    }
}
